package com.hejjon.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hejjon.core.domain.entity.LibraryCategory;
import com.hejjon.core.domain.vo.RecordRelTableInfoVO;
import com.hejjon.core.enumer.DeleteFlagEnum;
import com.hejjon.core.enumer.LibraryNodeTypeEnum;
import com.hejjon.core.exception.BusinessException;
import com.hejjon.core.utils.StrUtil;
import com.hejjon.core.utils.TreeBuilder;
import com.hejjon.domain.request.LibraryCategoryRequest;
import com.hejjon.domain.vo.LibraryCategoryVO;
import com.hejjon.mapper.LibraryCategoryMapper;
import com.hejjon.service.IFeaturePublishRecordService;
import com.hejjon.service.ILibraryCategoryService;
import com.hejjon.service.IRecordRelTableInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class LibraryCategoryServiceImpl extends ServiceImpl<LibraryCategoryMapper, LibraryCategory> implements ILibraryCategoryService {


    @Resource
    private LibraryCategoryMapper libraryCategoryMapper;

    @Resource
    private IFeaturePublishRecordService featurePublishRecordService;

    @Resource
    private IRecordRelTableInfoService recordRelTableInfoService;


    @Override
    public void addNode(LibraryCategoryRequest req) {
        verifyParams(req);
        String type = req.getType();
        LibraryNodeTypeEnum typeEnum = LibraryNodeTypeEnum.getByName(type);
        switch (typeEnum) {
            case FEATURE:
                String recordId = req.getRecordId();
                List<RecordRelTableInfoVO> relInfoList = recordRelTableInfoService.listByRecordId(recordId);
                List<LibraryCategory> entityList = new ArrayList<>();
                relInfoList.forEach(item -> {
                    LibraryCategory entity = new LibraryCategory();
                    entity.setId(StrUtil.getUUID());
                    entity.setName(item.getFeatureName());
                    entity.setLabel(item.getFeatureLabel());
                    entity.setType(LibraryNodeTypeEnum.FEATURE.getName());
                    entity.setRecordId(recordId);
                    entity.setFeatureId(item.getFeatureId());
                    entity.setMenuId(req.getMenuId());
                    entity.setParentId(req.getParentId());
                    entity.setHide(1);
                    entity.setCreateBy("admin");
                    entity.setCreateTime(LocalDateTime.now());
                    entity.setDeleteFlag(DeleteFlagEnum.NO.getCode());
                    entityList.add(entity);
                });
                libraryCategoryMapper.insertBatch(entityList);
                break;
            case CATEGORY:
                LibraryCategory category = createCategoryNode(req);
                libraryCategoryMapper.insert(category);
                break;
        }
    }

    /**
     * 创建目录节点
     */
    private LibraryCategory createCategoryNode(LibraryCategoryRequest req) {
        LibraryCategory entity = new LibraryCategory();
        entity.setId(StrUtil.getUUID());
        entity.setName(req.getName());
        entity.setType(LibraryNodeTypeEnum.CATEGORY.getName());
        entity.setMenuId(req.getMenuId());
        entity.setHide(1);
        entity.setLabel(req.getName());
        entity.setCreateBy("admin");
        entity.setCreateTime(LocalDateTime.now());
        entity.setDeleteFlag(DeleteFlagEnum.NO.getCode());
        entity.setParentId(StringUtils.isBlank(req.getParentId()) ? "-1" : req.getParentId());
        return entity;
    }

    @Override
    public List<LibraryCategoryVO> listLibraryCategoryTree() {

        List<LibraryCategoryVO> voList = libraryCategoryMapper.selectAll();
        TreeBuilder<LibraryCategoryVO, String> treeBuilder = new TreeBuilder<>();
        return treeBuilder.buildTree(voList);
    }


    /**
     * 校验入参
     */
    private void verifyParams(LibraryCategoryRequest req) {
        String name = req.getName();
        String menuId = req.getMenuId();
        String type = req.getType();
        LibraryNodeTypeEnum typeEnum = LibraryNodeTypeEnum.getByName(type);
        if (typeEnum == null) {
            throw new BusinessException("节点类型非法");
        }

        if (typeEnum == LibraryNodeTypeEnum.CATEGORY && StringUtils.isBlank(name)) {
            throw new BusinessException("目录节点名称不可为空!");
        }

        if (StringUtils.isBlank(menuId)) {
            throw new BusinessException("侧栏菜单ID不可为空!");
        }
    }


    @Override
    public boolean removeCategoryNode(String id) {
        Integer cnt = libraryCategoryMapper.existByParentId(id);
        if (cnt != null && cnt > 0) {
            throw new BusinessException("存在子节点不可删除!");
        }
        return libraryCategoryMapper.deleteById(id) > 0;
    }
}
